package com.cysyz.mylib.utils;

import org.apache.commons.io.FileUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collection;

/**
 * @File name : com.cysyz.mylib.utils.MyFileUtils
 * @Description :
 * @Creator : cuiyong
 * @CreateTime : 16/3/10 11:16
 * @Version : 1.0
 * @Update records:
 * 1.0 16/3/10 by cuiyong: Created
 * All rights served : SINA Corporation
 */
public class MyFileUtils extends FileUtils{
    public static void main(String[] args) throws IOException, InterruptedException {
//        String dirPath = "/Applications/workspace/idea_workspace/my-project/mylib/mylib-tools/utils/src/main/java/com/cysyz/mylib/utils";
//        Collection<File> files = FileUtils.listFiles(new File(dirPath), null, true);
//        for (File file : files) {
//            System.out.println(file.getPath());
//        }
//
//        File file = new File("/Users/cuiyong/test.txt");
//        MyFileUtils.changeFileContent(file, file, new FileContentHandler() {
//            @Override
//            public String handle(String fileContent) {
//                return fileContent + "shhsfd";
//            }
//        });


        String groupId = "com.sina.finance.paycenter.infrastructure";

        String artifactId = "waa";

//        String artifactId = System.getProperty("artifactId");
        Process exec = Runtime.getRuntime().exec("mvn archetype:generate -DgroupId="+ groupId + " -DartifactId=" + artifactId + " -DpackageName=" + groupId + "\n" +
                " -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false -DarchetypeCatalog=internal");
        int x = exec.waitFor();
        System.out.println(x);
        BufferedReader input = new BufferedReader(new InputStreamReader(exec.getInputStream()));
        String line = "";
        while ((line = input.readLine()) != null) {
            System.out.println(line);
        }
        input.close();
        if (x != 0) {
        }
    }

    public static void changeFileContent(File src, File target, FileContentHandler fileContentHandler) {
        try {
            String s = fileContentHandler.handle(FileUtils.readFileToString(src));
            FileUtils.write(target, s);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void changeFileContent(String dirPath, FileContentHandler fileContentHandler) {
        Collection<File> files = FileUtils.listFiles(new File(dirPath), null, true);
        for (File file : files) {
            MyFileUtils.changeFileContent(file, file, fileContentHandler);
        }
    }


    public static interface FileContentHandler {
        public String handle(String fileContent);
    }



}
